package io.sk.common.utils;

import org.springframework.util.StringUtils;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期转换器
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/5/9 15:21
 */
public class DateConvertEditor extends PropertyEditorSupport {

    private SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        if (StringUtils.hasText(text)) {

            try {

                if (text.indexOf(":") == -1 && text.length() == 10) {

                    setValue(this.dateFormat.parse(text));
                } else if (text.indexOf(":") > 0 && text.length() == 19) {

                    setValue(this.datetimeFormat.parse(text));
                } else if (text.indexOf(":") > 0 && text.length() == 21) {

                    text = text.replace(".0", "");
                    setValue(this.datetimeFormat.parse(text));
                } else {

                    throw new IllegalArgumentException("Could not parse date, date format is error ");
                }
            } catch (ParseException ex) {

                IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
                iae.initCause(ex);
                throw iae;
            }

        } else {

            setValue(null);
        }
    }

    /**
     * 时间相减
     *
     * @param startDate
     * @return
     */
    public static int calLastedTime(Date startDate, Date endDate) {
        long end = endDate.getTime();
        long start = startDate.getTime();
        int c = (int) ((end - start) / 1000);
        return c;
    }
}
